package cn.com.dcsgo.controller;

import cn.com.dcsgo.dto.BasketDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.BasketService;
import cn.com.dcsgo.utils.AuthUtil;
import cn.com.dcsgo.vo.CartMoneyVO;
import cn.com.dcsgo.vo.CartVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 购物车相关接口
 *
 * @author Dcsgo
 * @since 2025/11/14 15:12
 */
@RestController
@RequestMapping("/p/shopCart")
@Tag(name = "购物车相关接口")
public class CartController {
    @Resource
    private BasketService basketService;

    @GetMapping("prodCount")
    @Operation(summary = "获取购物车中商品数量", description = "商品购买数量总和")
    public Result<Integer> getProdTotal() {
        return Result.success(basketService.getProdTotal());
    }

    @PostMapping("changeItem")
    @Operation(summary = "添加或修改购物车中商品", description = "可用于减少购物车中某一商品的购买数量")
    public Result<Void> saveOrUpdateItem(@Validated @RequestBody BasketDTO basketDTO) {
        return Result.handle(basketService.saveOrUpdate(basketDTO));
    }

    @GetMapping("info")
    @Operation(summary = "获取购物车详细信息")
    public Result<CartVO> getCartVO() {
        return Result.success(basketService.getCartVO(AuthUtil.getOpenid()));
    }

    @PostMapping("totalPay")
    @Operation(summary = "获取购物车结算信息")
    public Result<CartMoneyVO> getCartMoneyVO(@RequestBody List<Long> basketIds) {
        return Result.success(basketService.getCartMoneyVO(AuthUtil.getOpenid(), basketIds));
    }

    @DeleteMapping("deleteItem")
    @Operation(summary = "删除购物项")
    public Result<Void> deleteItem(@RequestBody Set<Long> basketIds) {
        return Result.handle(basketService.removeBatchByIds(basketIds));
    }
}
